package com.gm.controller.rate;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.gm.common.Pager;
import com.gm.model.Base.Const;
import com.gm.model.po.Rate;
import com.gm.model.vo.ResMsg;
import com.gm.service.user.RateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;


/**
 * 汇率管理
 * Created by taojie on 17-6-21.
 */

@Controller
@RequestMapping("rate")
public class RateController {

    Logger logger = LoggerFactory.getLogger(RateController.class);

    @Autowired
    private RateService rateService;

    @RequestMapping("rateList.web")
    public ModelAndView rateList(Pager pager){
        ModelAndView view= new ModelAndView();
        PageList<Rate> rateList = rateService.getRateList(pager);
        Paginator paginator =rateList.getPaginator();
        view.addObject("rateList", rateList);
        view.addObject("paginator", paginator);
        view.setViewName("/rate/rate.jsp");
        return view;
    }

    @RequestMapping("gotoAddRate.web")
    public ModelAndView gotoAddRate(){
        ModelAndView view= new ModelAndView();
        view.setViewName("/rate/addRate.jsp");
        return view;
    }

    @RequestMapping(value = "addRate.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg addRate(Rate rate){
        logger.info("Method[addRate],rate[{}],remark[{}]",rate.getRate(),rate.getRemark());
        ResMsg resMsg= new ResMsg();
        int ret = rateService.addRate(rate);
        if(ret == 1){
            resMsg.setStatus(Const.RES_OK);
        }else{
            resMsg.setError("添加失败");
        }
        logger.info("Method[addRate],ret[{}]",ret);
        return resMsg;
    }

    @RequestMapping("gotoUpdateRate.web")
    public ModelAndView gotoUpdateUser(Integer rateId,Integer currentPage){
        ModelAndView view= new ModelAndView();
        Rate rate = rateService.selectByPrimaryKey(rateId);
        view.addObject("currentPage", currentPage);
        view.addObject("rate", rate);
        view.setViewName("/rate/updateRate.jsp");
        return view;
    }

    @RequestMapping(value = "updateRate.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg updateRate(Rate rate, Integer currentPage){
        logger.info("Method[updateRate],rate[{}],remark[{}]",rate.getRate(),rate.getRemark());
        ResMsg resMsg= new ResMsg();
        int ret = rateService.updateRate(rate);
        if(ret == 1){
            resMsg.setStatus(Const.RES_OK);
        }else{
            resMsg.setError("修改失败");
        }
        logger.info("Method[updateRate],ret[{}]",ret);
        return resMsg;
    }


    @RequestMapping("deleteRate.web")
    public ModelAndView deleteUser(Integer rateId,Integer currentPage,RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = rateService.deleteRate(rateId);
        if(ret==1){
            attr.addFlashAttribute("msg", "删除成功");
        }else{
            attr.addFlashAttribute("msg", "删除失败");
        }
        Pager pager = new Pager();
        pager.setCurrentPage(currentPage);
        List<Rate> userList = rateService.getRateList(pager);
        if(userList.size() == 0){
            currentPage = currentPage -1;
        }
        if(currentPage==0)currentPage=1;
        view.addObject("currentPage", currentPage);
        view.setViewName("redirect:/rate/rateList.web");
        return view;
    }
}
